package com.henry.customviewstudy.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dingmouren.layoutmanagergroup.banner.BannerLayoutManager;
import com.henry.customviewstudy.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by henry on 2018/6/7.
 */

public class BannerActivity extends AppCompatActivity {

    private ImageView mImg1, mImg2, mImg3, mImg4;
    private List<ImageView> mImgList = new ArrayList<>();
    ;
    private RecyclerView myRecycler1;
    private MyAdapter myAdapter;
    private int mLastSelectPosition = 0;
    private int mCurrentSelect = 0;
    private RecyclerView myRecycler2;
    private MyNewsAdapter myNewsAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        initView();
    }

    private void initView() {
        mImg1 = findViewById(R.id.img_1);
        mImg2 = findViewById(R.id.img_2);
        mImg3 = findViewById(R.id.img_3);
        mImg4 = findViewById(R.id.img_4);
        mImgList.add(mImg1);
        mImgList.add(mImg2);
        mImgList.add(mImg3);
        mImgList.add(mImg4);
        //图片轮播图
        myRecycler1 = findViewById(R.id.recycler1);
        myAdapter = new MyAdapter();
        BannerLayoutManager bannerLayoutManager = new BannerLayoutManager(this, myRecycler1, 4, OrientationHelper.HORIZONTAL);
        myRecycler1.setLayoutManager(bannerLayoutManager);
        myRecycler1.setAdapter(myAdapter);
        bannerLayoutManager.setOnSelectedViewListener(new BannerLayoutManager.OnSelectedViewListener() {
            @Override
            public void onSelectedView(View view, int position) {
                changeUI(position);
            }
        });
        changeUI(mCurrentSelect);
        //新闻轮播图
        myRecycler2 = findViewById(R.id.recycler2);
        myNewsAdapter = new MyNewsAdapter();
        BannerLayoutManager bannerNewsLayoutManager = new BannerLayoutManager(this, myRecycler2, 4, OrientationHelper.VERTICAL);
        bannerNewsLayoutManager.setTimeSmooth(400f);
        myRecycler2.setLayoutManager(bannerNewsLayoutManager);
        myRecycler2.setAdapter(myNewsAdapter);
    }

    //选中点 改变
    private void changeUI(int position) {
        if (position != mLastSelectPosition) {
            mImgList.get(position).setImageDrawable(getResources().getDrawable(R.drawable.circle_red));
            mImgList.get(mLastSelectPosition).setImageDrawable(getResources().getDrawable(R.drawable.cirque_gray));
            mLastSelectPosition = position;
        }
    }

    //图片适配器
    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private int[] imgs = {R.mipmap.banner_1, R.mipmap.banner_2, R.mipmap.banner_3, R.mipmap.banner_4};

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_banner, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.img.setImageResource(imgs[position % 4]);
        }

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView img;

            public ViewHolder(View itemView) {
                super(itemView);
                img = itemView.findViewById(R.id.img);
            }
        }
    }

    //新闻适配器
    private class MyNewsAdapter extends RecyclerView.Adapter<MyNewsAdapter.ViewHolder> {
        private String[] mTitles = {
                "小米8官方宣布有双路GPS,小米8、小米8SE发售时间曝光",
                "这样的锤子你玩懂了吗?坚果R1带来不一样的体验",
                "三星真的很爱酸苹果!新广告讽刺苹果手机电池降速事件",
                "双摄全面屏 游戏长续航 魅族科技发布魅蓝6T售799元起",
        };

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_banner_news, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tv_news.setText(mTitles[position % 4]);
        }

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private final TextView tv_news;

            public ViewHolder(View itemView) {
                super(itemView);
                tv_news = itemView.findViewById(R.id.tv_news);
            }
        }
    }
}
